<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title></title>
    <meta name="description" content="">
    <meta name="keywords" content="">
    <link href="" rel="stylesheet">
    <link rel="stylesheet" href="/plugns/layuitree/layui/css/layui.css">
    <link rel="stylesheet" type="text/css" href="//at.alicdn.com/t/font_1041315_0zq2yro0cfn.css">
    <style>
        .layui-table .value_col{
            text-align: center;
        }
    </style>
</head>

<body style="margin:10px 10px 0;">
<fieldset class="layui-elem-field">
    <legend>分类信息</legend>
    <div class="layui-field-box">
        <div class="layui-inline">
            <a class="layui-btn layui-btn-normal parentAdd_btn"><i class="iconfont icon-xinzeng"></i>添加顶级分类</a>
        </div>
    </div>
</fieldset>
<div class="layui-form menus_list">
    <div id="sortInfo"></div>
</div>
</body>
<script src="/plugns/layuitree/layui/layui.js"></script>
<script type="text/javascript" src="/js/jquery.min.js"></script>
<script type="text/javascript">
    var $,common;
    layui.config({
        base: '/js/common/' //假设这是你存放拓展模块的根目录
    }).use(['tree', 'layer','table','common'], function() {
        common = layui.common;
        var layer = layui.layer,
            $ = layui.jquery;

        var layout = [
            { name: '名称', treeNodes: true, headerClass: 'value_col'},
            {
                name: '排序',
                headerClass: 'value_col',
                colClass: 'value_col',
                style: 'width: 5%;text-align: center;',
                render:function(row){
                    return undefined === row.sort?"" : row.sort;
                }
            },
            {
                name: '创建时间',
                headerClass: 'value_col',
                colClass: 'value_col',
                style: 'width: 10%',
                render:function(row){
                    return undefined === row.createTime?"" : new Date(row.createTime).Format("yyyy-MM-dd hh:mm:ss");
                }
            },
            {
                name: '操作',
                headerClass: 'value_col',
                colClass: 'value_col',
                style: 'width: 30%;text-align: center;',
                render: function(row) {
                    return '<a class="layui-btn layui-btn-normal layui-btn-sm" onclick="addChild(' + row.id + ')"><i class="iconfont icon-xinzeng"></i>添加子分类</a>' +
                        '<a class="layui-btn layui-btn-normal layui-btn-sm" onclick="editChild(' + row.id + ')"><i class="iconfont icon-xiugai"></i>编辑分类</a>' +
                        '<a class="layui-btn layui-btn-danger layui-btn-sm" onclick="del(' + row.id + ')"><i class="iconfont icon-shanchu"></i>删除</a>';
                }
            }
        ];

        var setTree = function(data,layout){
            $("#sortInfo").empty();
            layui.treeGird({
                elem: '#sortInfo', //传入元素选择器
                nodes: data,
                layout: layout
            });
        };

        $(function(){
            $.post("treelist",function(res){
                if(res.success){
                    setTree(res.data,layout);
                }else{
                    layer.msg(res.message);
                }
            });
        });
        /**添加顶级分类*/
        $(".parentAdd_btn").click(function() {
            var url = "addUI";
            common.cmsLayOpen('添加顶级分类', url, '400px', '300px');

        });


    });

    function addChild(data){
        var url = "addUI?parentId="+data;
        common.cmsLayOpen('添加子分类', url, '400px', '300px');
    };

    function editChild(data){
        var url = "editUI?id="+data;
        common.cmsLayOpen('修改分类', url, '400px', '300px');
    };
    function del(data){
        layer.confirm("你确定要删除该分类么？这将会使得其下的所有子分类都删除",{btn:['是的,我确定','我再想想']},
            function(){
                $.post("delete",{"id":data},function (res){
                    if(res.success){
                        layer.msg("删除成功",{time: 1000},function(){
                            location.reload();
                        });
                    }else{
                        layer.msg(res.message);
                    }
                });
            }
        )
    };
    //格式化时间
    Date.prototype.Format = function (fmt) {
        var o = {
            "M+": this.getMonth() + 1, //月份
            "d+": this.getDate(), //日
            "h+": this.getHours(), //小时
            "m+": this.getMinutes(), //分
            "s+": this.getSeconds(), //秒
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
            "S": this.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    };
</script>

</html>